import pandas as pd  # 数据分析
import numpy as np  # 科学计算


# 去除含有空数据的样本


def deleteNaNData(df):
    # 参数检查
    if not isinstance(df, pd.DataFrame):
        raise TypeError('bad operand type,parameter must be a DataFrame')
    return df[df[df.columns].isin(['NaN'])]


def select1(df, x, prev, rear):
    # 参数检查
    if not isinstance(df, pd.DataFrame):
        raise TypeError('bad operand type,parameter must be a DataFrame')
    for index, row in df.iterrows():
        if prev <= row["" + x] < rear:
            df.drop([index])
    return df


def getRangeOfVariable(df, column_name):
    if not isinstance(df, pd.DataFrame):
        raise TypeError('bad operand type,parameter must be a DataFrame')
    if not isinstance(column_name, str):
        raise TypeError('bad operand type,column_name must be a string')
    res = []
    for x in df[column_name].values:
        if x not in res:
            res.append(x)
    return res


def replaceStrToNum(df, column_name, dict_replace=None):
    if not isinstance(df, pd.DataFrame):
        raise TypeError('bad operand type,parameter must be a DataFrame')
    if not isinstance(column_name, str):
        raise TypeError('bad operand type,column_name must be a string')
    # if not isinstance(dict_replace, dict):
    #     raise TypeError('bad operand type,dict_replace must be a dict')
    Range = getRangeOfVariable(df, column_name)
    if dict_replace is None:
        index = 1
        for i in Range:
            df.replace(i, index, inplace=True)
            index += 1
    else:
        for i in Range:
            df.replace(i, dict_replace[i], inplace=True)
    return df


